//
//  FCTransactionHistoryVC.m
//  FundChain
//
//  Created by 肖浩然的mac on 2018/3/2.
//  Copyright © 2018年 RUIXIN. All rights reserved.
//

#import "FCTransactionHistoryVC.h"
#import "FCTransactionHistoryModel.h"
#import "FCWebVC.h"
@interface FCTransactionHistoryVC (){
    UIView *_viewNav;
}

@property (strong, nonatomic)UILabel *moneyCountLable;
@property (strong, nonatomic)UILabel *usdLable;
@property (strong, nonatomic) UILabel *eqBtcLable;
@property (strong, nonatomic)  UILabel *sendAddressLable;

@property (strong, nonatomic)UIButton *copybtn;
@property (strong, nonatomic)UILabel *TransactionIdLable;
@property (strong, nonatomic) UILabel *nodeLable;
@property (strong, nonatomic)  UILabel *pendingLable;
@property (strong, nonatomic) UILabel *feeCountLable;
@property (strong, nonatomic) UILabel *timeLable;
@property (copy, nonatomic) NSString *priceStr;

/* sendLable */
@property (strong, nonatomic) UILabel *sendLable;
/* feeUsdLable*/
@property (strong, nonatomic)UILabel *feeUsdLable;
/* <#Annotation#> */
@property (strong, nonatomic) UILabel *feeLable;

/* <#Annotation#> */
@property (strong, nonatomic) UILabel *dateLable;

/* model */
@property (strong, nonatomic) FCTransactionHistoryDetailModel *model;
@end

@implementation FCTransactionHistoryVC


- (void)viewDidLoad {
    [super viewDidLoad];
    [FCLocalUserHelper saveIsMainVC:@"0"];
    self.view.backgroundColor = kWhiteColor;
    [self loadData];
    [self setNav];
    [self setupUI];
    
}

#pragma mark-设置UI
-(void)setupUI{
    
    //交易金额
    UILabel *moneyCountLable = [UILabel new];
    self.moneyCountLable = moneyCountLable;
    [self.view addSubview:moneyCountLable];
    [moneyCountLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_viewNav.mas_bottom).offset(80*KScreenHeightScale1334);
        make.left.mas_equalTo(30*KScreenWidthScale750);
    }];
    moneyCountLable.text = @"-0.01";
    moneyCountLable.font = Kfont(52);
    //usd
    UILabel *usdLable = [UILabel new];
    self.usdLable = usdLable;
    [self.view addSubview:usdLable];
    [usdLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(moneyCountLable).offset(-2);
        make.left.mas_equalTo(moneyCountLable.mas_right).offset(10*KScreenWidthScale750);
    }];
    usdLable.text = @"CCH";
    usdLable.font = Kfont(32);
    usdLable.textColor = kOrderColor;
    //等值多少比特币
    UILabel *eqBtcLable = [UILabel new];
    self.eqBtcLable = eqBtcLable;
    [self.view addSubview:eqBtcLable];
    [eqBtcLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(moneyCountLable).offset(-4);
        make.left.mas_equalTo(usdLable.mas_right).offset(20*KScreenWidthScale750);
    }];
//    eqBtcLable.text = @"(≈0.1324243535 BTC)";
    eqBtcLable.font = Kfont(28);
    eqBtcLable.textColor = kGrayColor;
   
    //发送
    UILabel *sendLable = [UILabel new];
    self.sendLable = sendLable;
    [self.view addSubview:sendLable];
    [sendLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(moneyCountLable.mas_bottom).offset(80*KScreenHeightScale1334);
        make.left.mas_equalTo(moneyCountLable);
    }];
    sendLable.text = Localized(@"发给");
    sendLable.font = Kfont(32);
    sendLable.textColor = kGrayColor;
    //发送地址
    UILabel *sendAddressLable = [UILabel new];
    self.sendAddressLable = sendAddressLable;
    [self.view addSubview:sendAddressLable];
    [sendAddressLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(sendLable.mas_bottom).offset(30*KScreenHeightScale1334);
        make.left.mas_equalTo(moneyCountLable);
        make.right.mas_equalTo(-50);
    }];
    sendAddressLable.text = @"Bsv85LeQdywGnoSu3VDhmfzBs17SBgZJdU";
    sendAddressLable.numberOfLines = 2;
    sendAddressLable.font = Kfont(28);
    sendAddressLable.textColor = kDarkBlackColor;
    //复制
    UIButton *copyBtn = [UIButton new];
    self.copybtn = copyBtn;
    [self.view addSubview:copyBtn];
    [copyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(sendAddressLable.mas_right).offset(40*KScreenWidthScale750);
        make.top.mas_equalTo(sendAddressLable);
    }];
    [copyBtn setBackgroundImage:[UIImage imageNamed:@"icon_copy"] forState:UIControlStateNormal];
    //交易
    UILabel *TransactionLable = [UILabel new];
    [self.view addSubview:TransactionLable];
    [TransactionLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(sendAddressLable.mas_bottom).offset(60*KScreenHeightScale1334);
        make.left.mas_equalTo(moneyCountLable);
    }];
    TransactionLable.text = Localized(@"交易ID");
    TransactionLable.font = Kfont(32);
    TransactionLable.textColor = kGrayColor;
    
    //交易ID
    UILabel *TransactionIdLable = [UILabel new];
    self.TransactionIdLable = TransactionIdLable;
    [self.view addSubview:TransactionIdLable];
    [TransactionIdLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(TransactionLable.mas_bottom).offset(20*KScreenHeightScale1334);
        make.left.mas_equalTo(moneyCountLable);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
    }];
    TransactionIdLable.text = @"3c3793cbfd4339128a32d38c4cf819c2cc72fe9399d067702fb90ad96b29cb08";
    TransactionIdLable.font = Kfont(28);
    TransactionIdLable.numberOfLines = 2;
    //复制交易ID
    UIButton *btn = [UIButton new];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(TransactionLable.mas_bottom).offset(20*KScreenHeightScale1334);
        make.left.mas_equalTo(moneyCountLable);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
        make.height.mas_equalTo(50);
    }];
    btn.backgroundColor = [UIColor clearColor];
    [btn addTarget:self action:@selector(alertViewClick) forControlEvents:(UIControlEventTouchUpInside)];
    
    //confirms
    UILabel *confirmsLable = [UILabel new];
    [self.view addSubview:confirmsLable];
    [confirmsLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(TransactionIdLable.mas_bottom).offset(60*KScreenHeightScale1334);
        make.left.mas_equalTo(moneyCountLable);
    }];
    confirmsLable.text = Localized(@"确认数");
    confirmsLable.font = Kfont(32);
    confirmsLable.textColor = kGrayColor;
    //节点node
    UILabel *nodeLable = [UILabel new];
    self.nodeLable = nodeLable;
    [self.view addSubview:nodeLable];
    [nodeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(confirmsLable.mas_bottom).offset(20*KScreenHeightScale1334);
        make.left.mas_equalTo(moneyCountLable);
    }];
    nodeLable.text = @"+35514";
    nodeLable.font = Kfont(36);
    nodeLable.textColor = kDarkBlackColor;
    //pending Confirmation
    UILabel *pendingLable = [UILabel new];
    self.pendingLable = pendingLable;
    [self.view addSubview:pendingLable];
    [pendingLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(confirmsLable.mas_bottom).offset(20*KScreenHeightScale1334);
        make.left.mas_equalTo(nodeLable.mas_right).offset(5*KScreenWidthScale750);
    }];
    pendingLable.text = @"(Confirmation)";
    pendingLable.font = Kfont(36);
    pendingLable.textColor = kRedOrangeColor;
    
    //fee
    UILabel *feeLable = [UILabel new];
    self.feeLable = feeLable;
    [self.view addSubview:feeLable];
    [feeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(pendingLable.mas_bottom).offset(60*KScreenHeightScale1334);
        make.left.mas_equalTo(moneyCountLable);
    }];
    feeLable.text = Localized(@"手续费");
    feeLable.font = Kfont(32);
    feeLable.textColor = kGrayColor;
    //feecount
    UILabel *feeCountLable = [UILabel new];
    self.feeCountLable = feeCountLable;
    [self.view addSubview:feeCountLable];
    [feeCountLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(feeLable.mas_bottom).offset(20*KScreenHeightScale1334);
        make.left.mas_equalTo(moneyCountLable);
    }];
    feeCountLable.text = @"0.00";
    feeCountLable.font = Kfont(36);
    feeCountLable.textColor = kDarkBlackColor;
    //feeUSD
    UILabel *feeUsdLable = [UILabel new];
    self.feeUsdLable = feeUsdLable;
    [self.view addSubview:feeUsdLable];
    [feeUsdLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(feeLable.mas_bottom).offset(20*KScreenHeightScale1334);
        make.left.mas_equalTo(feeCountLable.mas_right).offset(5);
    }];
    feeUsdLable.text = @"CCH";
    feeUsdLable.font = Kfont(36);
    feeUsdLable.textColor = kDarkBlackColor;
    
    
    //Date
    UILabel *dateLable = [UILabel new];
    self.dateLable = dateLable;
    [self.view addSubview:dateLable];
    [dateLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(feeCountLable.mas_bottom).offset(60*KScreenHeightScale1334);
        make.left.mas_equalTo(moneyCountLable);
    }];
    dateLable.text = Localized(@"日期");
    dateLable.font = Kfont(32);
    dateLable.textColor = kGrayColor;
    //time
    UILabel *timeLable = [UILabel new];
    self.timeLable = timeLable;
    [self.view addSubview:timeLable];
    [timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(dateLable.mas_bottom).offset(20*KScreenHeightScale1334);
        make.left.mas_equalTo(moneyCountLable);
    }];
    timeLable.text = @"2018-1-28 14:28:10";
    timeLable.font = Kfont(36);
    timeLable.textColor = kDarkBlackColor;
    //事件
    [copyBtn addTarget:self action:@selector(copyBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark-复制
-(void)copyBtnClick{
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    NSString *string = self.sendAddressLable.text;
    [pab setString:string];
    if (pab) {
        [self showAlert:Localized(@"已复制")];
    }else{
        
    }
    
    
}
-(void)alertViewClick{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    /*
     typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
     UIAlertControllerStyleActionSheet = 0,// 不能加输入框，其他一样
     UIAlertControllerStyleAlert // 可以添加输入框
     } 弹出类型;
     */
    
    __weak __typeof(self)wself = self;
    // 添加 AlertAction 事件回调（三种类型：默认，取消，警告）
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:Localized(@"复制和打开浏览器") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [wself openWebView];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:Localized(@"复制交易号码") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [wself copyTransactionId];
    }];
    UIAlertAction *errorAction = [UIAlertAction actionWithTitle:Localized(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    // cancel类自动变成最后一个，警告类推荐放上面
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [alertController addAction:errorAction];
    // 出现
    [self presentViewController:alertController animated:YES completion:^{
        
    }];
    
    
}
-(void)copyTransactionId{
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    NSString *string = self.TransactionIdLable.text;
    [pab setString:string];
    if (pab) {
        [self showAlert:Localized(@"已复制")];
    }else{
        
    }
}
-(void)openWebView{
    
   
    if (self.isOrder) {
        
        NSString *url;
        switch ([self.walletId integerValue]) {
            case 1001:
            {
                url = @"https://blockchain.info/search?search=";
            }
                break;
            case 1005:
            {
                url = @"https://chainz.cryptoid.info/ltc/search.dws?q=";
            }
                break;
            case 1006:
            {
                url = @"https://www.blocktrail.com/BCC/tx/";
            }
                break;
            case 1009:
            {
                url = @"https://cchexplorer.fund-chain.io/tx/";
            }
                break;
            case 1010:
            {
                
            }
                break;
            default:
                break;
        }
        NSString *urlStr = [NSString stringWithFormat:@"%@%@",url,self.TransactionIdLable.text];
        
        UIPasteboard *pab = [UIPasteboard generalPasteboard];
        NSString *string = self.TransactionIdLable.text;
        [pab setString:string];
        
        //调用safari打开网页
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
    }else{
        NSString *url;
        switch ([self.transactionHistoryDetailModel.walletId integerValue]) {
            case 1001:
            {
                url = @"https://blockchain.info/search?search=";
            }
                break;
            case 1005:
            {
                url = @"https://chainz.cryptoid.info/ltc/search.dws?q=";
            }
                break;
            case 1006:
            {
                url = @"https://www.blocktrail.com/BCC/tx/";
            }
                break;
            case 1009:
            {
                url = @"https://cchexplorer.fund-chain.io/tx/";
            }
                break;
            case 1010:
            {
                
            }
                break;
            default:
                break;
        }
        NSString *urlStr = [NSString stringWithFormat:@"%@%@",url,self.TransactionIdLable.text];
        
        UIPasteboard *pab = [UIPasteboard generalPasteboard];
        NSString *string = self.TransactionIdLable.text;
        [pab setString:string];
        
        //调用safari打开网页
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
    }
    

    
}
#pragma mark-自定义导航栏
-(void)setNav{
    self.navigationController.navigationBarHidden = YES;
    _viewNav =[UIView new];
    [self.view addSubview:_viewNav];
    [_viewNav mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                
                make.left.right.top.mas_equalTo(self.view.safeAreaInsets);
                make.height.mas_equalTo(88);
            }else{
                make.left.right.mas_equalTo(0);
                make.height.mas_equalTo(64);
                make.top.mas_equalTo(0);
            }
        }else{
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(64);
            make.top.mas_equalTo(0);
        }
        
    }];
    _viewNav.backgroundColor = kRedOrangeColor;
    
    UIButton *leftBtn = [UIButton new];
    [self.view addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15*KScreenWidthScale750);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    [leftBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    //返回
    UILabel *backLabel = [UILabel new];
    
    [_viewNav addSubview:backLabel];
    [backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_viewNav);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
    }];
    backLabel.text = Localized(@"交易历史");
    backLabel.textColor = [UIColor whiteColor];
    backLabel.font = KOverstrikingfont(36);
    //事件
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
#pragma mark-获取交易详情页(点击进去的列表)
-(void)loadData{
    
    
            _model = self.transactionHistoryDetailModel;
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.sendAddressLable.text = _model.address;
                    self.TransactionIdLable.text = _model.jyXID;
                    self.nodeLable.text = _model.confirms;
                    if ([_model.status isEqualToString:@"801"]) {
                        self.pendingLable.text = [NSString stringWithFormat:@"(%@)",Localized(@"未确认")];
                    }else if ([_model.status isEqualToString:@"802"]){
                        self.pendingLable.text = [NSString stringWithFormat:@"(%@)",Localized(@"确认中")];
                    }else if ([_model.status isEqualToString:@"803"]){
                        self.pendingLable.text = [NSString stringWithFormat:@"(%@)",Localized(@"已确认")];
                    }
                    self.timeLable.text = [FCLocalUserHelper getBabyDetailAge:_model.sendTime];
                    
                });
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    [self loadBalance];
                });
           

    
}
-(void)loadBalance{
    FCBalanceModel *balanceModel;
    if (self.isOrder) {
       balanceModel = [FCLocalUserHelper getFMDBBalanceData:self.walletId];
    }else{
        
        balanceModel = [FCLocalUserHelper getFMDBBalanceData:self.transactionHistoryDetailModel.walletId];
    }
    
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        NSString *amount;
        if ([self.transactionHistoryDetailModel.amount containsString:@"-"]) {
            self.sendLable.text = Localized(@"发给");
            amount =  [self.transactionHistoryDetailModel.amount stringByReplacingOccurrencesOfString:@"-" withString:@""];
        }else if ([self.transactionHistoryDetailModel.amount containsString:@"+"]){
            amount =  [self.transactionHistoryDetailModel.amount stringByReplacingOccurrencesOfString:@"+" withString:@""];
            self.sendLable.text = Localized(@"接收");
           
        }
       
    switch ([balanceModel.walletId integerValue]) {
        case 1001:
        {
            
            self.eqBtcLable.text = [NSString stringWithFormat:@"(≈%@ %@)",amount,Localized(@"BTC")];

        }
            break;
        case 1005:
        {
            
            self.eqBtcLable.text = [NSString stringWithFormat:@"(≈%@ %@)",amount,Localized(@"LTC")];
        }
            break;
        case 1006:
        {
            self.eqBtcLable.text = [NSString stringWithFormat:@"(≈%@ %@)",amount,Localized(@"BCH")];

        }
            break;
        case 1009:
        {
            
            self.eqBtcLable.text = [NSString stringWithFormat:@"(≈%@ %@)",amount,Localized(@"CCH")];
            self.eqBtcLable.hidden = YES;
            
        }
            break;
        case 1010:
        {
            
            self.eqBtcLable.text = [NSString stringWithFormat:@"(≈%@ %@)",amount,Localized(@"ZEC")];
            
            
        }
            break;
            
        default:
            break;
    }
    
    NSArray *arr = balanceModel.currentResult;
    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        FCCurrentResultModel *model = [FCCurrentResultModel mj_objectWithKeyValues:obj];
        if ([model.currentId isEqualToString:@"8001"]) {
            
           
            
            self.priceStr = model.price;
            
            if ([self.transactionHistoryDetailModel.isSend isEqualToString:@"1"]) {
               
                
                float result1 = [self.transactionHistoryDetailModel.amount floatValue]*[model.price floatValue];
                self.moneyCountLable.text = [NSString stringWithFormat:@"%0.2f",result1];;
                
            }else{
                
               
                
                float result1 = [self.transactionHistoryDetailModel.amount floatValue]*[model.price floatValue];
                self.moneyCountLable.text = [NSString stringWithFormat:@"%+0.2f",result1];;
            }
            
        }
        
        
    }];
        
        
        FCCurrentResultModel *currentResultModel = self.currentResultModel;
        
        if ([currentResultModel.walletId isEqualToString:self.transactionHistoryDetailModel.walletId]) {
            
            
            self.usdLable.text = currentResultModel.currentName;
           
            if ([self.transactionHistoryDetailModel.isSend isEqualToString:@"1"]) {
               
                float result = [currentResultModel.price floatValue]*[self.transactionHistoryDetailModel.amount floatValue];
                self.moneyCountLable.text = [NSString stringWithFormat:@"%.2f",result];
                
            }else{
                
                
                float result = [currentResultModel.price floatValue]*[self.transactionHistoryDetailModel.amount floatValue];
                self.moneyCountLable.text = [NSString stringWithFormat:@"+%.2f",result];
            }
            self.feeUsdLable.text = currentResultModel.currentName;
        }
        
        if ([self.transactionHistoryDetailModel.walletId isEqualToString:@"1009"]) {
            
            if ([self.transactionHistoryDetailModel.isSend isEqualToString:@"1"]) {
                
                self.moneyCountLable.text = [NSString stringWithFormat:@"%0.2f",[self.transactionHistoryDetailModel.amount floatValue]];
                
            }else{
                
                self.moneyCountLable.text = [NSString stringWithFormat:@"%+0.2f",[self.transactionHistoryDetailModel.amount floatValue]];
            }
            
            
            self.usdLable.text = @"CCH";
            self.feeUsdLable.text = @"CCH";
            self.feeCountLable.text = [NSString stringWithFormat:@"%.2f",[_model.fee floatValue]];
        }
    });
    
    
}
#pragma mark-返回
-(void)leftBtnClick{
    if (self.isOrder) {
        [FCLocalUserHelper pushMainTabbarController];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
}

@end
